/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 言初
 * Date: 2024-05-10
 * Time: 11:23
 */

class Test {
    int countreaders;//当前有多少线程正在读
    boolean read;
    boolean write;
    // 现在能不能读，现在能不能写（值为true，就表示现在能做某事； 值为false，就表示现在不能做某事）

    public Test() {
        countreaders = 0;
        read = true;
        write = true;
    }

    //读操作
    public synchronized void read(String name){
        while(read==false){
            try {
                System.out.println(name+" is waiting");
                // 等待notify发出
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println(name+"开始读");
        write=false;
        read=true;
        countreaders++;

    }
    //结束读操作
    public synchronized void endread(String name){
        countreaders--;
        if(countreaders==0){
            write=true;
        }
        notifyAll();
        System.out.println(name+"读结束");
    }

    // 写操作
    public synchronized void write(String name){
        while(write==false){
            try {
                System.out.println(name+" is waiting");
                // 等待notify发出
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(name+"正在写");
        read=false;
        write=false;
    }
    // 结束写操作
    public synchronized void endwrite(String name){
        read=true;
        write=true;
        notifyAll();
        System.out.println(name+"结束写");
    }
}



class Reader {
    String name;
    Test test;
    public Reader(String name,Test t){
        this.name=name;
        this.test=t;
    }

    public void reader() throws InterruptedException {
        System.out.println(name+"已创建");
        test.read(name);
        Thread.sleep(100);
        test.endread(name);
    }
}


class Writer {
    String name;
    Test test;
    public Writer(String name,Test t){
        this.name=name;
        this.test=t;
    }

    public void writer() throws InterruptedException {
        System.out.println(name+"已创建");
        test.write(name);
        Thread.sleep(100);
        // System.out.println("we will endwrite");
        test.endwrite(name);
    }
}

public class Last {

    public static void main(String[] args) {

        Test test = new Test();
        // 读者
        Reader r1 = new Reader("读者 1",test);
        Reader r2 = new Reader("读者 2",test);
        Reader r3 = new Reader("读者 3",test);
        // 写者
        Writer w1 = new Writer("写者 1",test);
        Writer w2 = new Writer("写者 2",test);
        Writer w3 = new Writer("写者 3",test);

        // 读者1
        Thread t1=new Thread( ()->{

            try {
                r1.reader();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        });

        //读者2
        Thread t2=new Thread( ()->{

            try {
                r2.reader();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        });

        //读者3
        Thread t3=new Thread( ()->{

            try {
                r3.reader();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        });

        // 写者1
        Thread t4=new Thread( ()->{

            try {
                w1.writer();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        });
        // 写者2
        Thread t5=new Thread( ()->{

            try {
                w2.writer();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        });

        // 写者3
        Thread t6=new Thread( ()->{

            try {
                w3.writer();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        });


        // 写者
        t4.start();
        t5.start();
        t6.start();
        // 读者
        t1.start();
        t2.start();
        t3.start();


    }
}